package com.campus_assistant.controller;


import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.campus_assistant.common.QueryPageParam;
import com.campus_assistant.common.StudentLoginParam;
import com.campus_assistant.common.ToVue;
import com.campus_assistant.config.db.DynamicDataSource;
import com.campus_assistant.pojo.db1.Student;
import com.campus_assistant.service.db1.StudentService;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private RestHighLevelClient client;

    //查询所有学生
    @GetMapping("/selectAllStudent")
    public ToVue selectAllStudent(){
        DynamicDataSource.dbName.set("db1");
        List<Student> stu_infoListAll = studentService.getStu_infoListAll();

        for (Student student : stu_infoListAll) {
            System.out.println(student);
        }
        return ToVue.suc(stu_infoListAll);
    }
    //分页查询所有学生
    @PostMapping("/selectAllStudentPaginate")
    public ToVue selectAllStudentPaginate(@RequestBody QueryPageParam query){
        DynamicDataSource.dbName.set("db1");
        List<Student> studentPaginate = studentService.getStudentPaginate(query.getPageNum()-1, query.getPageSize());//进行分页查询
        int total1 = studentService.getTotal();//学生表一共有多少条数据
        long total2=Long.valueOf(total1);//将int转为Long
        return ToVue.suc(total2,studentPaginate);
    }
    //根据姓名查询学生
    @GetMapping("/getStudentByName")
    public ToVue getStudentByName(@RequestParam("result")String name){
        DynamicDataSource.dbName.set("db1");
        List<Student> stu_infoListByName = studentService.getStu_infoListByName(name);
        for (Student student : stu_infoListByName) {
            System.out.println(student);
        }
        return ToVue.suc(null,stu_infoListByName);
    }

    //AI添加学生
    @GetMapping("/addStudent1")
    public ToVue addStudent1(@RequestParam("result")String result){
        DynamicDataSource.dbName.set("db1");
        String[] message = result.split(" ");//根据空格分隔信息

        String name=message[0];
        int age=Integer.valueOf(message[1]);
        String sex=message[2];
        String phone=message[3];
        String num=message[4];
        String advisor=message[5];
        Student student=new Student(0,name,age,sex,phone,num,advisor);//sId本身自增，随意传一个数进去
        System.out.println("添加学生：");
        System.out.println(student);
        studentService.addStu_info(student);
        return ToVue.suc(student);
    }
    //更新学生信息
    @RequestMapping("/updateStudent1")
    public ToVue updateStudent1(@RequestParam("message") String message,@RequestParam("name") String name){
        DynamicDataSource.dbName.set("db1");
        System.out.println("已经进入updateStudent");
        String[] newMessage = message.split(" ");//根据空格分隔信息
        for (String s : newMessage) {//后台检查
            System.out.println(s);
        }

        String newName=newMessage[0];
        System.out.println(newName);
        int age=Integer.valueOf(newMessage[1]);
        String sex=newMessage[2];
        String phone=newMessage[3];
        String num=newMessage[4];
        String advisor=newMessage[5];
        int id=studentService.getIdByName(name);
        System.out.println(id);
        Student student=new Student(id,newName,age,sex,phone,num,advisor);//sId本身自增，随意传一个数进去
        System.out.println(student);
        studentService.updateStu_info(student);//调用方法修改学生
        return ToVue.suc();
    }
    @PostMapping("/updateStudent2")
    public ToVue updateStudent2(@RequestBody Student student){
        DynamicDataSource.dbName.set("db1");
        studentService.updateStu_info(student);
        return ToVue.suc();
    }
    @PostMapping("/updateStudent3")
    public ToVue updateStudent3(@RequestParam("studentName") String studentName){
        DynamicDataSource.dbName.set("db1");
        List<Student> stu_infoListByName = studentService.getStu_infoListByName(studentName);
        return ToVue.suc(stu_infoListByName);
    }
    @PostMapping("/updateStudent4")
    public ToVue updateStudent4(@RequestBody Student student){
        DynamicDataSource.dbName.set("db1");
        int i = studentService.updateStu_info(student);
        if(i==0){
            return ToVue.fail();
        }else {
            return ToVue.suc();
        }
    }
    //删除学生信息
    @RequestMapping("/deleteStudent1")
    public ToVue deleteStudent1(@RequestParam("result")String name){
        System.out.println("已进入删除模块");
        System.out.println("学生姓名为："+name);
        DynamicDataSource.dbName.set("db1");
        int id=studentService.getIdByName(name);//获取到姓名对应的id
        studentService.deleteStu_infoById(id);
        return ToVue.suc();
    }
    @RequestMapping("/deleteStudent2")
    public ToVue deleteStudent2(@RequestParam("id")int id){
        DynamicDataSource.dbName.set("db1");
        studentService.deleteStu_infoById(id);
        return ToVue.suc();
    }
    //学生登录
    @PostMapping("/studentLogin")
    public ToVue studentLogin(@RequestBody StudentLoginParam param){
        DynamicDataSource.dbName.set("db1");
        Student theStudent=null;
        if(!studentService.getStu_infoListByName(param.getUsername()).isEmpty()
                &&studentService.getStudentByNum(param.getPassword())!=null){
            theStudent=studentService.getStudentByNum(param.getPassword());
        }
        return theStudent!=null?ToVue.suc(theStudent):ToVue.fail();

    }
}
